//package com.unknownman.nft.transaction.subscribe;
//
//import com.lianziyou.chain.common.config.BscWeb3jConfig;
//import com.unknownman.nft.constants.ChainContractConstants;
//import com.unknownman.nft.constants.LogConstants;
//import com.unknownman.nft.dto.ChainContractComposition;
//import com.unknownman.nft.entity.TContractConfig;
//import com.unknownman.nft.entity.TContractLogs;
//import com.unknownman.nft.rabbitmq.send.TranLogDirectSend;
//import com.unknownman.nft.service.TChainConfigService;
//import com.unknownman.nft.service.TContractLogsService;
//import lombok.extern.slf4j.Slf4j;
//import org.cloud.utils.CollectionUtil;
//import org.cloud.utils.SpringContextUtil;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.util.Assert;
//import org.springframework.util.StringUtils;
//import org.web3j.protocol.Web3j;
//import org.web3j.protocol.core.methods.response.EthBlock;
//import org.web3j.protocol.core.methods.response.Log;
//import org.web3j.protocol.core.methods.response.TransactionReceipt;
//
//import java.math.BigInteger;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//
///**
// * bsc链监听交易
// */
//@Slf4j
//public class BscTranFilter extends BaseTranFilter{
//
//    private TChainConfigService chainConfigService;
//    private TContractLogsService contractLogsService;
//    private TranLogDirectSend tranLogDirectSend;
//    private Web3j web3j;
//
//    private ChainContractComposition chainContractComposition;
//
//    private Map<String, TContractConfig> contractConfigMap;
//
//    public BscTranFilter(ChainContractComposition chainContractComposition) {
//        super(BscWeb3jConfig.BscWeb3j.class);
//        this.chainContractComposition = chainContractComposition;
//        //查出启用的合约地址
//        contractConfigMap = this.chainContractComposition.getContractList().stream()
//                .filter(cont -> cont.getContractStatus() == ChainContractConstants.ContractStatusConstants.NORMAL.getStatus())
//                .collect(Collectors.toMap((contractConfig) -> contractConfig.getContractAddress().toUpperCase(), Function.identity()));
//
//        chainConfigService = SpringContextUtil.getBean(TChainConfigService.class);
//        contractLogsService = SpringContextUtil.getBean(TContractLogsService.class);
//        tranLogDirectSend = SpringContextUtil.getBean(TranLogDirectSend.class);
//        web3j = SpringContextUtil.getBean(BscWeb3jConfig.BscWeb3j.class);
//    }
//
//    @Override
//    public void subscribeCallBack(TransactionReceipt transactionReceipt) throws Exception {
//        List<Log> logs = transactionReceipt.getLogs();
//        if (CollectionUtil.single().isEmpty(logs)){
//            return;
//        }
//        EthBlock.Block block = web3j.ethGetBlockByHash(transactionReceipt.getBlockHash(), false).send().getBlock();
//        Long timestamp = block.getTimestamp().longValue() * 1000;
//        ArrayList<TContractLogs> saveLogList = new ArrayList<>();
//        for (Log log : logs) {
//            //判断是不是监听的日志
//            TContractConfig contractConfig = contractConfigMap.get(log.getAddress().toUpperCase());
//            if (contractConfig == null){
//                continue;
//            }
//            if (!contractLogsService.existsByHashAndIndex(transactionReceipt.getTransactionHash(), log.getLogIndex().longValue())){
//                TContractLogs contractLogs = new TContractLogs();
//                contractLogs.setContractConfigId(contractConfig.getContractConfigId());
//                contractLogs.setContractAddress(log.getAddress());
//                contractLogs.setTransFrom(transactionReceipt.getFrom());
//                if (!StringUtils.isEmpty(transactionReceipt.getContractAddress())){
//                    contractLogs.setTransTo(transactionReceipt.getContractAddress());
//                }else {
//                    contractLogs.setTransTo(transactionReceipt.getTo());
//                }
//                contractLogs.setTransHash(transactionReceipt.getTransactionHash());
//                contractLogs.setIndexForBlock(log.getLogIndex().longValue());
//                contractLogs.setBlockTimestamp(new Date(timestamp));
//                List<String> topics = log.getTopics();
//                contractLogs.setFirstTopic(topics.get(0));
//                if (topics.size() >= 2){
//                    contractLogs.setSecondTopic(topics.get(1));
//                }
//                if (topics.size() >= 3){
//                    contractLogs.setThirdTopic(topics.get(2));
//                }
//                if (topics.size() >= 4){
//                    contractLogs.setFourthTopic(topics.get(3));
//                }
//                contractLogs.setLogData(log.getData());
//                contractLogs.setStatus(LogConstants.ContractLogStatus.UNTREATED.getStatus());
//                saveLogList.add(contractLogs);
//                //消息发送
//                tranLogDirectSend.convertAndSendLog(log);
//            }
//        }
//        if (CollectionUtil.single().isNotEmpty(saveLogList)){
//            boolean b = contractLogsService.saveBatch(saveLogList);
//            Assert.isTrue(b, "subscribeCallBack-" + chainContractComposition.getChainSymbol() + "：log保存失败");
//        }
//
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void updateLastBlock(BigInteger block, boolean success) {
//        chainConfigService.updateLastBlock(chainContractComposition.getChainConfigId(), block.longValue(), success);
//    }
//}
